package com.czt.mp3recorder;

import android.graphics.drawable.AnimationDrawable;
import android.media.MediaPlayer;
import android.text.TextUtils;

import java.io.IOException;

public class MP3Player {

    private static MP3Player mInstance;

    private MediaPlayer mediaPlayer;
    private AnimationDrawable animationDrawable;

    private OnMP3PlayerListener onMP3PlayerListener;
    private onMP3PlayerStartListener onMP3PlayerStartListener;

    public static synchronized MP3Player getInstance() {
        if (mInstance == null) {
            mInstance = new MP3Player();
        }
        return mInstance;
    }

    private MP3Player() {
        mediaPlayer = new MediaPlayer();
    }

    /**
     * 开始播放语音
     */
    public void startPlay(String filePath, AnimationDrawable ad) {
        if (!TextUtils.isEmpty(filePath)) {
            if (mediaPlayer != null && mediaPlayer.isPlaying()) {
                if (animationDrawable != null) {
                    animationDrawable.stop();
                    animationDrawable.selectDrawable(0);
                }
                mediaPlayer.stop();
                mediaPlayer.release();
                mediaPlayer = null;
            }
            mediaPlayer = new MediaPlayer();
            try {
                mediaPlayer.setDataSource(filePath);
                mediaPlayer.prepareAsync();
                mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                    @Override
                    public void onCompletion(MediaPlayer mPlayer) {
                        if (animationDrawable != null) {
                            animationDrawable.stop();
                            animationDrawable.selectDrawable(0);
                        }
//                        mediaPlayer.release();
//                        mediaPlayer = null;
                        if (onMP3PlayerListener != null) {
                            onMP3PlayerListener.onCompletion(mediaPlayer);
                        }
                    }
                });
                mediaPlayer.setOnSeekCompleteListener(new MediaPlayer.OnSeekCompleteListener() {
                    @Override
                    public void onSeekComplete(MediaPlayer mediaPlayer) {
                        if (onMP3PlayerListener != null) {
                            onMP3PlayerListener.onSeekComplete(mediaPlayer);
                        }
                    }
                });

                mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                    @Override
                    public void onPrepared(MediaPlayer mp) {
                        mp.start();
                        if (onMP3PlayerStartListener != null) {
                            onMP3PlayerStartListener.onStart(mediaPlayer);
                        }
                    }
                });

                mediaPlayer.setOnErrorListener(new MediaPlayer.OnErrorListener() {
                    @Override
                    public boolean onError(MediaPlayer mp, int what, int extra) {
                        if (onMP3PlayerListener != null) {
                            onMP3PlayerListener.onError(mediaPlayer);
                        }
                        return true;
                    }
                });

                // mediaPlayer.start();
                if (ad != null) {
                    this.animationDrawable = ad;
                    this.animationDrawable.start();
                }
            } catch (IOException e) {
                e.printStackTrace();
                mediaPlayer = null;
            }
        }
    }


    public void prepare(String filePath, AnimationDrawable ad) {
        mediaPlayer = new MediaPlayer();
        if (!TextUtils.isEmpty(filePath)) {
            try {
                mediaPlayer.setDataSource(filePath);
                mediaPlayer.prepareAsync();
                mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                    @Override
                    public void onCompletion(MediaPlayer mPlayer) {
                        if (animationDrawable != null) {
                            animationDrawable.stop();
                            animationDrawable.selectDrawable(0);
                        }
//                        mediaPlayer.release();
//                        mediaPlayer = null;
                        if (onMP3PlayerListener != null) {
                            onMP3PlayerListener.onCompletion(mediaPlayer);
                        }
                    }
                });
                mediaPlayer.setOnSeekCompleteListener(new MediaPlayer.OnSeekCompleteListener() {
                    @Override
                    public void onSeekComplete(MediaPlayer mediaPlayer) {
                        if (onMP3PlayerListener != null) {
                            onMP3PlayerListener.onSeekComplete(mediaPlayer);
                        }
                    }
                });

                mediaPlayer.setOnErrorListener(new MediaPlayer.OnErrorListener() {
                    @Override
                    public boolean onError(MediaPlayer mp, int what, int extra) {
                        if (onMP3PlayerListener != null) {
                            onMP3PlayerListener.onError(mediaPlayer);
                        }
                        return true;
                    }
                });
            } catch (IOException e) {
                e.printStackTrace();
                mediaPlayer = null;
            }
        }
    }


    public void initMediaPlayerListener() {
        mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mPlayer) {
                if (onMP3PlayerListener != null) {
                    onMP3PlayerListener.onCompletion(mediaPlayer);
                }
            }
        });
        mediaPlayer.setOnSeekCompleteListener(new MediaPlayer.OnSeekCompleteListener() {
            @Override
            public void onSeekComplete(MediaPlayer mediaPlayer) {
                if (onMP3PlayerListener != null) {
                    onMP3PlayerListener.onSeekComplete(mediaPlayer);
                }
            }
        });

        mediaPlayer.setOnErrorListener(new MediaPlayer.OnErrorListener() {
            @Override
            public boolean onError(MediaPlayer mp, int what, int extra) {
                if (onMP3PlayerListener != null) {
                    onMP3PlayerListener.onError(mediaPlayer);
                }
                return true;
            }
        });
    }

    public void prepare(String filePath) {
        MP3Player.getInstance();
        try {
            mediaPlayer.setDataSource(filePath);
            initMediaPlayerListener();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (IllegalStateException e) {
            e.printStackTrace();
        }
    }

    public void start() {
        if (mediaPlayer != null) {
            mediaPlayer.prepareAsync();
            mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                @Override
                public void onPrepared(MediaPlayer mp) {
                    mp.start();
                    if (onMP3PlayerStartListener != null) {
                        onMP3PlayerStartListener.onStart(mp);
                    }
                }
            });
        }
    }

    public void start(String file) {
        if (mediaPlayer != null) {
            try {
                mediaPlayer.setDataSource(file);
                mediaPlayer.prepareAsync();
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
    }


    public void restart() {
        if (mediaPlayer != null) {
            mediaPlayer.start();
        }
    }

    public void pause() {
        if (mediaPlayer != null) {
            mediaPlayer.pause();
        }
    }

    public void stop() {
        if (mediaPlayer != null) {
            mediaPlayer.stop();
            mediaPlayer.release();
        }
    }


    public void destroy() {
        if (mediaPlayer != null) {
            mediaPlayer.release();
            mediaPlayer = null;
        }
    }

    public void pausePlay() {
        if (mediaPlayer != null && mediaPlayer.isPlaying()) {
            if (animationDrawable != null) {
                animationDrawable.stop();
                animationDrawable.selectDrawable(0);
            }
            mediaPlayer.pause();
        }
    }

    public void stopPlay() {
        if (mediaPlayer != null && mediaPlayer.isPlaying()) {
            if (animationDrawable != null) {
                animationDrawable.stop();
                animationDrawable.selectDrawable(0);
            }
            mediaPlayer.stop();
            mediaPlayer.release();
            mediaPlayer = null;
        }
    }

    public boolean isPlaying() {
        if (mediaPlayer != null) {
            return mediaPlayer.isPlaying();
        }
        return false;
    }

    public MediaPlayer getMediaPlayer() {
        return mediaPlayer;
    }

    public void setOnMP3PlayerListener(OnMP3PlayerListener onMP3PlayerListener) {
        this.onMP3PlayerListener = onMP3PlayerListener;
    }

    public void setOnMP3PlayerStartListener(onMP3PlayerStartListener onMP3PlayerStartListener) {
        this.onMP3PlayerStartListener = onMP3PlayerStartListener;
    }

    /**
     * 监听
     */
    public interface OnMP3PlayerListener {

        void onCompletion(MediaPlayer mediaPlayer);

        void onSeekComplete(MediaPlayer mediaPlayer);

        void onError(MediaPlayer mediaPlayer);

    }

    public interface onMP3PlayerStartListener {
        void onStart(MediaPlayer mediaPlayer);
    }
}
